/**
 * @description 字符串
 * @author 张航
 * @date 2020-03-18 12:19:51
 * @version V1.0.0
 */

/**
 * @functionName camelize
 * @param {String} str 待转换的字符串
 * @return {String} 转换后的字符串
 * @description 横线转驼峰命名
 * @author 张航
 * @date 2019-07-10 14:09:21
 * @version V1.0.0
 */
export const camelize = (str = '') => {
  const camelizeRE = /-(\w)/g
  return str.replace(camelizeRE, function (_, c) {
    return c ? c.toUpperCase() : ''
  })
}

/**
 * @functionName hyphenate
 * @param {String} str 待转换的字符串
 * @return {String} 转换后的字符串
 * @description 驼峰转横线命名
 * @author 张航
 * @date 2019-07-10 14:11:38
 * @version V1.0.0
 */

export const hyphenate = (str = '') => {
  const hyphenateRE = /\B([A-Z])/g
  return str.replace(hyphenateRE, '-$1').toLowerCase()
}

/**
 * @functionName capitalize
 * @param {String} str 待转换的字符串
 * @return {String} 转换后的字符串
 * @description 字符串首位大写
 * @author 张航
 * @date 2019-07-10 14:13:54
 * @version V1.0.0
 */
export const capitalize = (str = '') => {
  return str.charAt(0).toUpperCase() + str.slice(1)
}

/**
 * @functionName trim
 * @param {String} string 需要处理的字符串
 * @return {String} 返回结果
 * @description 去掉字符串首尾空格
 * @author 张航
 * @date 2020-05-06 17:42:43
 * @version V1.0.0
 */
export const trim = (string = '') => {
  return string.replace(/^[\s\uFEFF]+|[\s\uFEFF]+$/g, '')
}

/**
 * @functionName urlParam
 * @param {*|String|Number|Boolean|Array|Object} data 查询参数
 * @return {*|String|Number|Boolean|Array|Object} 转换后的字符串
 * @description 查询参数对象转换为字符串
 * @author 张航
 * @date 2020-09-14 16:23:41
 * @version V1.0.0
 */
export const urlParam = (data) => {
  let url = ''
  for (const key in data) {
    const value = data[key] !== undefined ? data[key] : ''
    url += `&${key}=${encodeURIComponent(value)}`
  }
  return url ? `?${url.substring(1)}` : ''
}
